from django.core.cache import cache
from django.shortcuts import render
from rest_framework.serializers import ModelSerializer
from  rest_framework.views import APIView
from rest_framework.response import Response

from home import homeser
from home.homeser import NavSerializer, BannerSerializer
from home.models import NavModel, BannerModel


# Create your views here.

class Top(APIView):
    def get(self, request):
        top=NavModel.objects.filter(position=True,isshow=True).order_by('order')
        ser=NavSerializer(top,many=True)
        return Response({'message':"success",'data':ser.data,'code':200})

class Foot(APIView):
    def get(self, request):
        # 先设置一个缓存的key
        key='footnav'
        # 判断缓存中是否存在这个key
        cacheres=cache.get(key)
        if cacheres:
            return Response({'message':"success",'data':cacheres,'code':200})
        foot=NavModel.objects.filter(position=False,isshow=True).order_by('order')
        ser=NavSerializer(foot,many=True)
        cache.set(key,ser.data,60*60*24)
        return Response({'message':"success",'data':ser.data,'code':200})

class Banner(APIView):
    def get(self, request):
        banner=BannerModel.objects.filter(isshow=True).order_by('order')
        ser=BannerSerializer(banner,many=True)
        return Response({'message':"success",'data':ser.data,'code':200})
